package com.atm.dao;

import com.atm.pojo.Manager;
import com.atm.utill.DBUtils;
import com.atm.utill.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;


/**
 * 管理员用户持久层操作
 */
public class AdminUserDao {
    /**
     * 管理员登录
     * @param name 名字
     * @param password 密码
     * @return 管理员
     */
    public Manager findManager(String name,String password) {
//        Connection connection = DBUtils.getConnection();
//        PreparedStatement preparedStatement = null;
//        ResultSet resultSet = null;
//        Manager manager=null;
//        try {
//            preparedStatement = connection.prepareStatement("select * from manage where name = ? and password =?");
//            preparedStatement.setString(1,name);
//            preparedStatement.setString(2,password);
//             resultSet = preparedStatement.executeQuery();
//            if (resultSet.next()) {
//                manager= new Manager();
//                manager.setId(resultSet.getInt(1));
//                manager.setName(resultSet.getString(2));
//                manager.setPassword(resultSet.getString(3));
//            }
//            return manager;
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }finally {
//            DBUtils.release(connection,preparedStatement,resultSet);
//        }
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        String sql = "select * from manage where name = ? and password =?";
        try {
            return queryRunner.query(sql,new BeanHandler<>(Manager.class),name,password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
